package com.vdm.action;

import com.vdm.model.SalesmanInfo;
import com.vdm.model.UserInfo;
import com.vdm.service.SalesmanService;
import com.vdm.util.Constants;
import com.vdm.util.ErrorConstants;
import com.vdm.util.ReturnMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "salesman")
public class SalesmanController {

    @Autowired
    SalesmanService salesmanService;

    @RequestMapping(value = "/toSalesmanList")
    public String toSalesmanList(){return "salesman/salesmanList";}

    @ResponseBody
    @RequestMapping(value = "/salesmanList",method = RequestMethod.GET)
    public Map<String,Object> salesmanList(){
        List<SalesmanInfo> userList = salesmanService.salesmanInfoList();
        int count = salesmanService.getSalesmanInfoCount();
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @ResponseBody
    @RequestMapping(value="/updateSalesmanStatus",method=RequestMethod.POST)
    public Map<String,Object> updateSalesmanStatus(@RequestParam(value = "list[]")List<Integer> ids, Integer type){
        if(salesmanService.updateSalesmanInfoStatus(ids, type)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @ResponseBody
    @RequestMapping(value="/updateSalesmanByPk",method=RequestMethod.POST)
    public Map<String,Object> updateSalesmanByPk(SalesmanInfo salesmanInfo){
        if(salesmanService.updateByPk(salesmanInfo)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @RequestMapping(value = "/toAddSalesman")
    public String toAddSalesman(){return "salesman/addSalesman";}

    @RequestMapping(value = "/addSalesman" , method = RequestMethod.POST)
    public String addSalesman(SalesmanInfo salesmanInfo, HttpSession session){
        UserInfo user = (UserInfo) session.getAttribute(Constants.CURRENT_USER);
        salesmanInfo.setCreateBy(user.getLoginName());
        salesmanService.insertSelective(salesmanInfo);
        return "redirect:toSalesmanList";
    }

}
